﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Collections;

namespace dasFX.VSTime.API
{
    public interface IEventRecordRepository : IRepository<EventRecord, VSTimeEntities>
    { 
        
    }

    public interface IEventSummaryRepository : IRepository<vwEventDuration, VSTimeEntities>
    { 
    }

    public class EventRecordRepository : Repository<EventRecord, VSTimeEntities>, IEventRecordRepository
    {
        public EventRecordRepository()
            : base(new VSTimeEntities(new EntityConnection(SQLSubmitEvent.BuildEntityConnectionString(string.Empty))))
        { }

       
    }

    public class EventSummaryRepository : Repository<vwEventDuration, VSTimeEntities>, IEventSummaryRepository
    {
        public EventSummaryRepository()
            : base(new VSTimeEntities(new EntityConnection(SQLSubmitEvent.BuildEntityConnectionString(string.Empty))))
        { }

         public IQueryable<EventSummary> EventTotalsBySolution(int SolutionID)
         {
             var summary = DoQuery("vwEventDuration").Where(x => x.SolutionID == SolutionID);
             var events = from e in summary
                          group e by new { e.SolutionID, e.EventDetail } into g
                          let TotalMinutes = g.Sum(x => x.EventDuration)
                          orderby TotalMinutes descending
                          select new EventSummary
                          {
                              SolutionID = g.Key.SolutionID,
                              Name = g.Key.EventDetail,
                              Seconds = TotalMinutes,
                              Minutes = TotalMinutes / 60.0,
                              Hours = (TotalMinutes / 60.0) / 60.0                           
                          };
             return events;  
         }

         public IQueryable<EventSummary> EventTotalsBySolution(string SolutionFileName)
         {
             SolutionRepository solutionRepository = new SolutionRepository();
             Solution solution = solutionRepository.DoQuery("Solution").Where(x => x.SolutionFileName == SolutionFileName).FirstOrDefault();
             int solutionID = 0;

             if (solution != null)
             {
                 solutionID = solution.ID;
             }



             return null;
         }

    }
}
